from Rufe.models import UserProfile, Products
from rest_framework import serializers


class UserProfileSerializer(serializers.ModelSerializer):
    # 利用drf中的validators验证username是否唯一
    # username = serializers.CharField(required=True, allow_blank=False,
    #                                  validators=[UniqueValidator(queryset=UserProfile.objects.all(),
    #                                                              message='用户已经存在')])
    # password = serializers.CharField(
    #     style={"input_type": "password"}, help_text="密码", label="密码", write_only=True,
    # )

    class Meta:
        model = UserProfile
        fields = "__all__"
        # fields = ['uid', 'username', 'phone', 'gender']


class ProductsModelSerializer(serializers.HyperlinkedModelSerializer):
    """
        Products序列化
    """
    # 序列化检索外键的值:CharField(source='<本model中的外键>.<外键指向的model的相应属性>')
    Category_name = serializers.ReadOnlyField(source='Category.name')

    class Meta:
        model = Products
        fields = ('url', 'Item_Code', 'Brand', 'Item_Season',
                  'Item_Season', 'Item_Gender', 'Category_name',
                  'ReatilPrice', 'Sug_Cost', 'Tag1')

